/**
 * @author LKQ
 * @date 2021/11/28 14:47
 * @description 双重循环，时间复杂度较高。
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        String[] str = {"flower","flow","flight"};
        System.out.println(solution.longestCommonPrefix(str));
    }

    public String longestCommonPrefix(String[] strs) {
        String shortest = strs[0];
        String ans = "";
        // 找到最短的字符串
        for (String str : strs) {
            if (str.length() < shortest.length()) {
                shortest = str;
            }
        }
        for (int i = 0; i < shortest.length(); i++) {
            boolean flag = true;
            String temp = shortest.substring(0, i+1);
            for (int j = 0; j < strs.length; j++) {
                if (!temp.equals(strs[j].substring(0, i+1))) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                ans = "" +temp;
            }
            if (!flag) {
                break;
            }
        }
        return ans;
    }
}
